package org.wyy.vm.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.wyy.vm.entity.User;
import org.wyy.vm.service.IUserService;
import org.wyy.vm.service.impl.UserServiceImpl;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

/**
 * @author w
 *
 */
public class Reg extends JFrame {
	
	private IUserService userService = new UserServiceImpl();

	private static final long serialVersionUID = -4546569840645271274L;
	private JPanel contentPane;
	private JTextField idField;
	private JTextField nameField;
	private JTextField pwdField;

	/**
	 * Create the frame.
	 */
	public Reg() {
		setTitle("注册");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("编  号");
		lblNewLabel.setBounds(82, 72, 54, 15);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("用户名");
		lblNewLabel_1.setBounds(82, 121, 54, 15);
		contentPane.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("密  码");
		lblNewLabel_2.setBounds(82, 170, 54, 15);
		contentPane.add(lblNewLabel_2);
		
		idField = new JTextField();
		idField.setBounds(178, 69, 190, 21);
		contentPane.add(idField);
		idField.setColumns(10);
		
		nameField = new JTextField();
		nameField.setBounds(178, 118, 190, 21);
		contentPane.add(nameField);
		nameField.setColumns(10);
		
		pwdField = new JTextField();
		pwdField.setBounds(178, 167, 190, 21);
		contentPane.add(pwdField);
		pwdField.setColumns(10);
		
		JButton btnNewButton = new JButton("注册");
		btnNewButton.setBounds(275, 229, 93, 23);
		btnNewButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				Long id = null;
				try {
					id = Long.valueOf(idField.getText());
				} catch (Exception e2) {
					// TODO: handle exception
				}
				
				String name = nameField.getText();
				String pwd = pwdField.getText();
				
				if(id == null) {
					JOptionPane.showMessageDialog(getThis(), "编号格式错误", "提示信息", JOptionPane.WARNING_MESSAGE);
				}
				else if(name == null || "".equals(name.trim())) {
					JOptionPane.showMessageDialog(getThis(), "用户名格式错误", "提示信息", JOptionPane.WARNING_MESSAGE);
				}else if(pwd == null || "".equals(pwd.trim())) {
					JOptionPane.showMessageDialog(getThis(), "密码格式错误", "提示信息", JOptionPane.WARNING_MESSAGE);
				}else {
					userService.reg(new User(id, name, pwd));
					dispose();
					new Login().setVisible(true);
				}
			}
		});
		contentPane.add(btnNewButton);
	}
	
	JFrame getThis() {
		return this;
	}
}